package com.example.car;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHandler extends SQLiteOpenHelper {

	// All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 6;
 
    // Database Name
    private static final String DATABASE_NAME = "carManager";
 
    // Cars table name
    private static final String TABLE_CARS = "Cars";
 
    // Cars Table Columns names
    private static final String KEY_RANK				= "rank";
	private static final String KEY_NAME				= "name";
    private static final String KEY_AUTOMAKE			= "autoMake";
    private static final String KEY_AUTOMODEL			= "autoModel";
    private static final String KEY_ENGINE				= "engine";
    private static final String KEY_OBJECTTYPE			= "objectType";
    private static final String KEY_PHOTO				= "photo";
    private static final String KEY_SET					= "ofset";
    private static final String KEY_STATS				= "stats";
    private static final String KEY_TAGS				= "tags";
    private static final String KEY_YEAR				= "year";
    private static final String KEY_TOPSPEED			= "topSpeed";
    private static final String KEY_ACCELERATION100		= "acceleration100";
    private static final String KEY_ACCELERATION160		= "acceleration160";
    private static final String KEY_POWER				= "power";
    private static final String KEY_TORQUEFORCE			= "torqueForce";
    private static final String KEY_TORQUERPM			= "torqueRPM";
    private static final String KEY_CREDITNAME			= "creditName";
    private static final String KEY_CREDITURL			= "creditURL";
    
    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    
	public DatabaseHandler(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_CARS_TABLE = "CREATE TABLE " + TABLE_CARS + "("
				+ KEY_RANK 				+ " INTEGER, "
				+ KEY_NAME 				+ " TEXT PRIMARY KEY,"
                + KEY_AUTOMAKE 			+ " TEXT,"
                + KEY_AUTOMODEL 		+ " TEXT,"
                + KEY_ENGINE		 	+ " TEXT,"
                + KEY_OBJECTTYPE 		+ " TEXT,"
                + KEY_PHOTO 			+ " TEXT,"
                + KEY_SET 				+ " TEXT,"
                + KEY_STATS 			+ " TEXT,"
                + KEY_TAGS 				+ " TEXT,"
                + KEY_YEAR 				+ " TEXT,"
                + KEY_TOPSPEED 			+ " TEXT,"
                + KEY_ACCELERATION100 	+ " TEXT,"
                + KEY_ACCELERATION160 	+ " TEXT,"
                + KEY_POWER 			+ " TEXT,"
                + KEY_TORQUEFORCE 		+ " TEXT,"
                + KEY_TORQUERPM 		+ " TEXT,"
                + KEY_CREDITNAME 		+ " TEXT,"
                + KEY_CREDITURL 		+ " TEXT"
				+ ")";
        db.execSQL(CREATE_CARS_TABLE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_CARS);
 
        // Create tables again
        onCreate(db);
	}
	
	// Adding new car
    void addCar(Car car) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        
		
		values.put(KEY_RANK, car._rank);
		values.put(KEY_NAME, car._name);
		values.put(KEY_AUTOMAKE, car._automake);
		values.put(KEY_AUTOMODEL, car._automodel);
		values.put(KEY_ENGINE, car._engine);
		values.put(KEY_OBJECTTYPE, car._objecttype);
		values.put(KEY_PHOTO, car._photo);
		values.put(KEY_SET, car._set);
		values.put(KEY_STATS, car._stats);
		values.put(KEY_TAGS, car._tags);
		values.put(KEY_YEAR, car._year);
		values.put(KEY_TOPSPEED, car._topspeed);
		values.put(KEY_ACCELERATION100, car._acceleration100);
		values.put(KEY_ACCELERATION160, car._acceleration160);
		values.put(KEY_POWER, car._power);
		values.put(KEY_TORQUEFORCE, car._torqueforce);
		values.put(KEY_TORQUERPM, car._torquerpm);
		values.put(KEY_CREDITNAME, car._creditname);
		values.put(KEY_CREDITURL, car._crediturl);
 
        // Inserting Row
        db.insert(TABLE_CARS, null, values);
        db.close(); // Closing database connection
    }
 
    // Getting single car
    Car getCar(String name) {
        SQLiteDatabase db = this.getReadableDatabase();
 
        Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CARS + " WHERE " + KEY_NAME + "=" + name, null);
        if (cursor != null)
            cursor.moveToFirst();
 
        Car car = new Car(
        				Integer.parseInt(cursor.getString(0)),
						cursor.getString(1) ,
						cursor.getString(2) ,
						cursor.getString(3) ,
						cursor.getString(4) ,
						cursor.getString(5) ,
						cursor.getString(6) ,
						cursor.getString(7) ,
						cursor.getString(8) ,
						cursor.getString(9) ,
						cursor.getString(10),
						cursor.getString(11),
						cursor.getString(12),
						cursor.getString(13),
						cursor.getString(14),
						cursor.getString(15),
						cursor.getString(16),
						cursor.getString(17),
						cursor.getString(18)
						);
        // return car
		cursor.close();
		db.close();
        return car;
    }
 
     // Getting All Cars
    public List<Car> getAllCars() {
        List<Car> carList = new ArrayList<Car>();
        // Select All Query
        String selectQuery = "SELECT * FROM " + TABLE_CARS
        					+ " ORDER BY " + KEY_RANK;
 
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Car car = new Car();
				
				car._rank 			 =  Integer.parseInt(cursor.getString(0));
				car._name 			 =  cursor.getString(1);
				car._automake 		 =  cursor.getString(2);
				car._automodel 		 =  cursor.getString(3);
				car._engine 		 =  cursor.getString(4);
				car._objecttype 	 =  cursor.getString(5);
				car._photo 			 =  cursor.getString(6);
				car._set 			 =  cursor.getString(7);
				car._stats 			 =  cursor.getString(8);
				car._tags 			 =  cursor.getString(9);
				car._year 			 =  cursor.getString(10);
				car._topspeed 		 =  cursor.getString(11);
				car._acceleration100 =  cursor.getString(12);
				car._acceleration160 =  cursor.getString(13);
				car._power 			 =  cursor.getString(14);
				car._torqueforce 	 =  cursor.getString(15);
				car._torquerpm 		 =  cursor.getString(16);
				car._creditname 	 =  cursor.getString(17);
				car._crediturl 		 =  cursor.getString(18);
                
                // Adding car to list
                carList.add(car);
            } while (cursor.moveToNext());
        }
		
		cursor.close();
		db.close();
        // return car list
        return carList;
    }
    
 /*
    // Updating single car
    public int updateCar(Car car) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, car.getName());
        values.put(KEY_PH_NO, car.getPhoneNumber());
 
        // updating row
		int ret = db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
                new String[] { String.valueOf(car.getID()) });
		db.close();
        return ret;
		
    }
 
    // Deleting single car
    public void deleteCar(Car car) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
                new String[] { String.valueOf(car.getID()) });
        db.close();
    }
 */
    // Getting cars Count
    public int getCarsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_CARS;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        int count = cursor.getCount();
        Log.d("LOG","getCarsCount = " + count);
        cursor.close();
        db.close();
        // return count
        return count;
    }
    
    //This funtion will update car rank, and return number of row affected
    public int setCarRank(String carname, int rank)
    {
    	SQLiteDatabase db = this.getWritableDatabase();
    	ContentValues values = new ContentValues();
    	values.put(KEY_RANK, rank);
    	  
    	int ret = db.update(TABLE_CARS, values, KEY_NAME + "=?" , new String[]{carname});
    	db.close();
    	return ret;
    }
    
	
}
